package com.proj.hot.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.proj.hot.GlobalParams;
import com.proj.hot.R;

import org.json.JSONObject;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * 扫码支付页，里面只有微信和支付宝二维码
 *
 */
public class PayOneActivity extends BaseActivity {


    TextView pay_price_text;
    ImageView wxcode,alcode;
    String contentid;


    boolean isPay=false;
    boolean isfinish=false;


    private Handler handler =  new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 0){

                if(isPay) {
                    Toast.makeText(PayOneActivity.this, "支付成功！", Toast.LENGTH_LONG).show();
                    finish();
                }else{
                    if(!isfinish){
                        getIsPay();
                        handler.sendEmptyMessageDelayed(0,3000);
                    }

                }
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payone);
        pay_price_text=(TextView) findViewById(R.id.pay_price_text);
        wxcode=(ImageView) findViewById(R.id.wxcode);
        alcode=(ImageView) findViewById(R.id.alcode);

//        intent.putExtra("wxpic",wxpic);
//        intent.putExtra("alpic",alpic);
//        intent.putExtra("price",price);
//        intent.putExtra("contentid",contentid);
        String wxpic=getIntent().getStringExtra("wxpic");
        String alpic=getIntent().getStringExtra("alpic");
        String price=getIntent().getStringExtra("price");
        contentid=getIntent().getStringExtra("contentid");



        ImageLoader.getInstance().displayImage(wxpic,wxcode);
        ImageLoader.getInstance().displayImage(alpic,alcode);

        pay_price_text.setText("￥"+price);

        handler.sendEmptyMessageDelayed(0,3000);

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        isfinish=true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        isfinish=true;
    }

    private void getIsPay(){

        try {



            /**
             * ---------------body------------------
             */
            JSONObject body = new JSONObject();
//            {"sn":"设备号","conentid":"购买影片Id","accountid":"用户id"}
            body.put("sn", GlobalParams.SN);
            body.put("contentid", contentid);
            body.put("accountid", GlobalParams.user_id);



            RequestParams params = new RequestParams(GlobalParams.reqHost + "attestation/ispay");
            params.setAsJsonContent(true);
            params.setBodyContent(body.toString().trim());

            /**
             * ---------------send------------------
             */
            x.http().post(params, new org.xutils.common.Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String s) {
                    try {
                        JSONObject jData=new JSONObject(s);

                        JSONObject respbody=jData.getJSONObject("respbody");
                        JSONObject data=respbody.getJSONObject("data");
                        if (data.getInt("ispay")==1){
                            isPay=true;
                        }


                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onFinished() {

                }

                @Override
                public void onCancelled(CancelledException e) {

                }

                @Override
                public void onError(Throwable throwable, boolean b) {

                }
            });


        }catch (Exception e){
            e.printStackTrace();
        }

    }




}
